﻿<%--*AutoLoadForm 自定义表单解释Page
* 创建：江怀玉
* 日期:
* 版本
*修改日期：2012-3-15
*修改说明：
*--%>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoLoadForm.aspx.cs" Inherits="iSun.WebApp.AutoLoadForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <%--
    <link href="../Styles/uni-form/uni-form.css" rel="stylesheet" type="text/css" />
    <link href="../Styles/uni-form/qunit.css" rel="stylesheet" type="text/css" />
    <link href="../Styles/uni-form/default.uni-form.css" rel="stylesheet" type="text/css" />
    --%>
    <link rel="stylesheet" type="text/css" href="/styles/default/calendar.css" />
    <link href="/styles/table_style.css" rel="stylesheet" type="text/css" />
    <link href="/scripts/jquery/datetimepicker/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="/scripts/jquery/formValidator.2.5.4.1/css/template.css" type="text/css" />
    <link rel="stylesheet" href="/scripts/jquery/formValidator.2.5.4.1/css/validationEngine.jquery.css" type="text/css" />
    <style type="text/css">
        body
        {
            height: 100%;
            font: 12px/18px Tahoma, Helvetica, Arial, Verdana, "\5b8b\4f53" , sans-serif;
            color: #51555C;
            background: #ececec;
        }
        
        .textReadOnly
        {
            border-bottom-style: none;
            border-bottom-color: transparent;
            border-right-style: none;
            border-top-color: transparent;
            border-top-style: none;
            border-right-color: transparent;
            border-left-style: none;
            border-left-color: transparent;
        }
        .selected
        {
            background-color: #5d99da;
            color: White;
        }
        .ui-timepicker-div .ui-widget-header
        {
            margin-bottom: 8px;
        }
        .ui-timepicker-div dl
        {
            text-align: left;
        }
        .ui-timepicker-div dl dt
        {
            height: 25px;
            margin-bottom: -25px;
        }
        .ui-timepicker-div dl dd
        {
            margin: 0 10px 10px 65px;
        }
        .ui-timepicker-div td
        {
            font-size: 90%;
        }
        .ui-tpicker-grid-label
        {
            background: none;
            border: none;
            margin: 0;
            padding: 0;
        }
        .ui_tpicker_hour_label, .ui_tpicker_minute_label, .ui_tpicker_second_label, .ui_tpicker_millisec_label, .ui_tpicker_time_label
        {
            padding-left: 20px;
        }
    </style>
    <%--<script src="../Scripts/jquery-1.6.min.js" type="text/javascript"></script>--%>
    <script src="../scripts/jquery/formValidator.2.5.4.1/js/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../scripts/jquery.json-2.2.js" type="text/javascript"></script>
    <script src="../scripts/jquery.form.js" type="text/javascript"></script>
    <script src="../scripts/jquery.calendar.js" type="text/javascript"></script>
    <script src="../scripts/datePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../scripts/jquery.upload.js" type="text/javascript"></script>
    <script src="../scripts/combinButton.js" type="text/javascript"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <script src="../scripts/formReForm.js" type="text/javascript"></script>
    <script src="../scripts/Appjs/CheckModel.js" type="text/javascript"></script>
    <%-- <script src="../Scripts/hrEmployee.js" type ="text/javascript"></script>--%>
    <script src="../Scripts/Jquery/ComBox/babu.common.js" type="text/javascript"></script>
    <script src="../Scripts/Jquery/ComBox/babu.combobox.js" type="text/javascript"></script>
    <%--  <script src="../Scripts/Jquery.form.datatable.js" type="text/javascript"></script>--%>
    <script src="../scripts/Jquery/datetimepicker/jquery-ui.js" type="text/javascript"></script>
    <script src="../scripts/Jquery/datetimepicker/jquery-ui-slide.min.js" type="text/javascript"></script>
    <script src="../scripts/Jquery/datetimepicker/jquery-ui-timepicker-addon.js" type="text/javascript"></script>
    <script src="../scripts/Jquery/formValidator.2.5.4.1/js/jquery.validationEngine.js"
        type="text/javascript"></script>
    <script src="../scripts/Jquery/formValidator.2.5.4.1/js/languages/jquery.validationEngine-zh_CN.js"
        type="text/javascript"></script>
</head>
<body>
    <form id="form1" class="uniForm" runat="server">
    <%--<input id="Button1" type="button" value="button" onclick="seve1()" />--%>
    <div class="fieldForm" id="fieldForm">
        <asp:Literal ID="FormTemplate" runat="server"></asp:Literal>
    </div>
    </form>
    <form id="form2" action="AjaxData/DataSave.aspx">
    <textarea style="display: none" id="jsonDataContent" rows="2" cols="2" name="jsonDataContent"></textarea>
    <textarea style="display: none" id="Details" rows="2" cols="2" name="Details"></textarea>
    </form>
</body>
<script type="text/javascript">
        var url = "AjaxData/DataSave.aspx",
        entity = "<% =EntityName %>";
        var const_KeyValue = "<% =KeyValue %>";
        var Parent = parent;
        <asp:Literal ID="EditTemplate" runat="server"></asp:Literal>
        <asp:Literal ID="MasterID" runat="server"></asp:Literal>
        //alert(instance[0].EntityName);
        var status = '<%=Status%>'
        if(status.toString() == "3")
        {
            var item  = $("#form1").SetControlReadOnly();
        }
        ///-------------------------------------
        ///供调用方法
        ///-------------------------------------
        //        function seve1(){
        //        alert("测试");
        //        
        //        }


   //输入字符限制
    function confine(msg, oMaxNum, maxNum) {
        var iLen = 0;
        for (i = 0; i < msg.val().length; i++)
            iLen += msg.val().charAt(i).charCodeAt() > 255 ? 1 : 0.5;
        maxNum - Math.floor(iLen) >= 0 ? sendFlag = 0 : sendFlag = 1;
        if (sendFlag == 0) {
            oMaxNum.html("还能输入<em>" + Math.abs(maxNum - Math.floor(iLen)) + "</em>字");
            oMaxNum.css("color", "");
        }
        else if (sendFlag == 1) {
            oMaxNum.html("已经超过<em>" + Math.abs(maxNum - Math.floor(iLen)) + "</em>字");
            oMaxNum.css("color", "#f60");
        }
    };
        function Save(){
            if(typeof(fnCustomCheck)!="undefined")
            {
                var _msg = fnCustomCheck();
                if(_msg!="")
                {
                    alert(_msg);
                    return;
                }
            }
            var btnSubmit=parent.document.getElementById('btnCensorSubmit')
            if(checkInputData()=="0")
             {
                  if( typeof btnSubmit!= "undefined")
                  {
                    btnSubmit.disabled = false;
                  }
                   return;
             }

           var isSuccess=jQuery('#form1').validationEngine('validate');
           if(isSuccess==false)
           {
                 if( typeof btnSubmit!= "undefined")
                  {
                    btnSubmit.disabled = false;
                  }
                  return;
           }
           //debugger
             //在保存前，将数据从UI绑定到实体中
            if(typeof(_DataTable)!="undefined")
            {
               if(fnCheckEmpty()==false)
               {
                    alert("明细数据没有录入");
               }
               _DataTable.GetData();
//                for(var i=0;i<_DataTable.Rows.length;i++){
//                            var trs = $("#id"+i);
//                            trs.SetFormValueToObject(_DataTable.Rows[i]);
//                }
            }

            if(typeof(fnCustomSave)=="function")
            {
                fnCustomSave();
                return ;
            }

            if(typeof(trySave)=="function")
            {
                setValue();
                if(typeof(_DataTable)!="undefined")
                {
                    $("#Details").val($.toJSON(_DataTable.Rows).replace(/'/g,"[B]"));
                    //$("#Details").val($.toJSON(instance[0]));
                    //instance[0].datarows = _DataTable.Rows;
                }
                var json =$.toJSON(instance[0]).replace(/'/g,"[B]");
                if(typeof(_DataTable)!="undefined")
                {
                    if(_DataTable.Rows.length>0){
                        json = json+"[@@@]"+$.toJSON(_DataTable.Rows);
                    }
                }
                //var json =$.toJSON(instance[0]).replace(/'/g,"[B]").replace(/\n/g,'[BR]');
                $("#jsonDataContent").val(json);
                trySave();
                return;
            }

           
            fnSystemSave();
        }

        ///------------------------------------
        //系统的保存方法
        ///------------------------------------
        var action = '<% =DataAction %>';
        function setValue()
        {
           <asp:Literal ID="jSave" runat="server"></asp:Literal>
           if(typeof(_DataTable)=="undefined"){
               $("#fieldForm").SetFormValueToObject(instance[0]);
           }
           else
           {
               $("#Master").SetFormValueToObject(instance[0]);
           }
           //instance[0].datarows =decodeURIComponent($.toJSON(_DataTable.Rows));
           //debugger
           //instance[0]= $("form1").extractObject(); 
        }
        function fnSystemSave()
        {
            ///初始化相关参数
            setValue();
            $("#jsonDataContent").val($.toJSON(instance[0]));
            var SaveUrl =url+"?r=" + Math.random() + "&entity=" + entity + "&action="+action+"&json=" + encodeURI($("#jsonDataContent").val().replace(/&/g,"[A]").replace(/'/g,"[B]"));
             if(typeof(checkData)=="function")
            {
                 if(checkData()=="0")
                 {
                     return;
                 }
             }
            ///调用验证
            if(typeof(fnCheckData)=="function")
            {
                var msg =fnCheckData(instance[0]);
                if(!msg=="")
                {
                    alert(msg);
                    return;
                }
            }
            //发送保存请求
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: responseSuccess
                ,
                failure: responseFailure
            }, null);
        }
       
       ///初始化相关信息
       $(document).ready(function() {
       
           <asp:Literal ID="jInitControl" runat="server"></asp:Literal>
          
           //编辑状态
           if(getPar1("id")!="")
           {
               $("#form1").JSONToForm($.toJSON(instance[0]));
           }
           if(typeof (userPropertyValues) != "undefined")
           {
                $("#form1").JSONToFormDefaultPropertyValues($.toJSON(userPropertyValues[0]));
           }
   

           $("#form1").LabelModel();
           //debugger;
           if( typeof (MasterFormRoles) != "undefined")
           {
             if(MasterFormRoles!=null)
             {
                  var fieldForm=$("#fieldForm");
                  if( typeof fieldForm!= "undefined")
                  {
                    var _date=$("input[type=Date]");
                    initControls(_date,"Date");
                    var _datetime=$("input[type=Time]");
                    initControls(_datetime,"Time");
                  }

                 if( typeof (MasterFormRoles.ReadonlyPropertyValue) != "undefined")
                 {
                    $("#form1").ReadonlyModel(MasterFormRoles.ReadonlyPropertyValue);
                }
                if( typeof (MasterFormRoles.RequiredPropertyValue) != "undefined")
                {
                    $("#form1").RequiredModel(MasterFormRoles.RequiredPropertyValue);
                }
                if( typeof (MasterFormRoles.HidePropertyValue) != "undefined")
                {
                    $("#form1").HideModel(MasterFormRoles.HidePropertyValue);
                }
             }
           }

          //showMessage();
          //debugger;
     
          if(typeof(fnInitFormCustomFunc)=="function")
          {
             fnInitFormCustomFunc();
          }

          jQuery("#form1").validationEngine();
        });
         

        // 初始时间控件
         var initControls = function(matchArray,ControlType)
         {
             if (matchArray) {
                    for (var i = 0; i < matchArray.length; i++) 
                    {
                      
                       var cId=matchArray[i].id;
                       var controlID='#'+cId;
                      if(!IsReadonlyModel(cId))
                      {
                           switch(ControlType)
                            {
                             case 'Date':
                                    $(matchArray[i]).attr("onclick", "WdatePicker()");
                                    var oldclass= $(matchArray[i]).attr("class");
                                   if( typeof oldclass!= "undefined")
                                   {
                                         $(matchArray[i]).attr("class", "Wdate "+oldclass);
                                   }
                                   else
                                   {
                                        $(matchArray[i]).attr("class", "Wdate");
                                    }
                            
                              
                               break;
                             case 'Time':
                                      $(controlID).timepicker({
	                                   hourGrid: 4,
	                                   minuteGrid: 10
                                   });
                                  $(matchArray[i]).attr("class", "Wdate");
                               break;
                            }
                        }
                    }
                }
     
         }

        function IsReadonlyModel(controlID)
        {
             var isReadonly=false;
            if( typeof (MasterFormRoles.ReadonlyPropertyValue) != "undefined")
             {
                    var _obj=MasterFormRoles.ReadonlyPropertyValue;
                    $.each(_obj, function (property, value) 
                    {
                       if (value != "") 
                       { 
                          if(value==controlID)
                          {
                           isReadonly=true;
                           return false;
                          }
                       }
                    });  
             }
             return isReadonly;
        }
   

         var formReForm;
         function doReform() {
            // load the css
            formReForm = new FormReForm('fieldForm');
            //formReForm.loadformReFormCss();
            // do the reform with default settings
            formReForm.doReForm();

        }
        
        
        var trySave = function()   
        {
            // argument formId can be the id or name attribute value of the
            // HTML form, or an HTML form object.
            setValue();
            if(typeof(fnCheckData)!="undefined")
            {
                var msg = fnCheckData();
                if(typeof(msg)!="undefined")
                {
                   alert(msg);
                   return;
                } 
            }
            var formObject = document.getElementById('form2');
            YAHOO.util.Connect.setForm(formObject);
            // This example facilitates a POST transaction.  The POST data(HTML form)
            // are initialized when calling setForm(), and it is automatically
            // included when calling asyncRequest.
            //debugger
            if(typeof(_DataTable)!="undefined"){
                var cObj = YAHOO.util.Connect.asyncRequest('POST',url+"?from=AutoLoadForm&isBatch=true&flag=1&action="+action+"&entity="+entity,callback);
            }
            else
            {
               var cObj = YAHOO.util.Connect.asyncRequest('POST',url+"?from=AutoLoadForm&flag=1&action="+action+"&entity="+entity,callback);
            }
        }


        var responseSuccess = function (res) {
                    try {
                    //debugger
                        var str = res.responseText;
                        //var obj = eval(str);
                    
                        if(str=="1"){
                            if(getPar1("workflowid")=="")
                            {
                                alert("数据保存成功");
                            }else{
                                alert("表单保存交成功");
                            }
                           // if(action=='insert')
                           //{
                                //parent.fnAddRow(instance[0]);
                                if(typeof( parent.ReLoadData)=="function"){
                                     var Title=$("#bpmTitle").val();
                                     parent.closeWin();     
                                     parent.ReLoadData(const_KeyValue,Title);                              
                                }
                                else{
                                //debugger
                                  var Title=$("#bpmTitle").val();
                                  parent.parent.closeWin();   
                                  parent.parent.ReLoadData(const_KeyValue,Title);
                                         
                                }
                            //}else{
                            //    parent.ReLoadData(const_KeyValue);
                               //parent.fnUpdateRow(instance[0]);
                           // }
                           
                        }
                        else
                        {
                            if(str!=""){
                                showMessage();
                                alert(str+"\n"+"数据保存失败!");
                            }
                        }
                    }
                    catch (e) {

                    }
                    finally {
                    }
        };
        var responseFailure =function (error) {
                    alert("数据错误：" + error.statusText);
        }
 
        // Create a callback object that defines success and failure
        // handlers, and instructs Connection Manager to use the
        // Flash transport by defining the xdr property and setting
        // it to true.
       var  callback = {
            success:responseSuccess,
            failure:responseFailure
        };
 
       var SetControlValue=function(o)
       {
            $.each(o, function (property, value) {
            //try {
            value = value + "";
            // find the control and set value
            if (property != "") {
                var control = $("[name='" + property + "']");
                var strValue = value.replace(/@1@1@1/g, "'");
                strValue = strValue.replace(/@2@2@2/g, '"');
                strValue = strValue.replace(/@@@@/g, "\n");
                strValue = strValue.replace(/[BR]/g, "\n");
                control.val(strValue);
                try {
                    $('#' + property).combobox('setValue', strValue);
                } catch (Error) { }
            }
            //  };
        });
       }
</script>
<script src="../Scripts/appjs/model.<% =EntityName %>.js" type="text/javascript"
    language="javascript"></script>
<script type="text/javascript" language="javascript">
    //调用初始化控件功能
    if (typeof (fnInitControl) == "function") {
        fnInitControl();
    }
</script>
<script src="../Scripts/Jquery/uni-form/uni-form-validation.jquery.js" type="text/javascript"></script>
<%if (!string.IsNullOrEmpty(DetailsJson))
  { %>
<script src="../Scripts/Jquery.form.datatable.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
        _DataTable.FExpression="<% = FKExpression %>";
        $(document).keypress(function (e) {
            //alert(e);
            //alert(e.which );
            if (e.ctrlKey && e.which == 13) {
                alert("");
            }
        });
        <%=DetailsInstance %> 

        if ($("#Details").length > 0) {
            _DataTable._DataBind();
            //_DataTable.AddRow();
        }
        $(document).bind('keydown', 'ctrl+1',function (evt){
            if(evt.ctrlKey && evt.which==49){
                _DataTable.AddRow();
            }
            if(evt.ctrlKey && evt.which==46){
                _DataTable.DeleteRow();
            }
        });

        setTimeout(fnSelected,300);
</script>
<%} %>
<%--<input type="button" value="SAVE" onclick="Save()" />--%>
</html>
